package com.gitee.feizns.quickstart.validation.validator;

import com.gitee.feizns.dynamic.Strings;
import com.gitee.feizns.quickstart.validation.constraints.Account;
import lombok.RequiredArgsConstructor;

import javax.validation.ConstraintValidatorContext;
import java.util.regex.Pattern;

/**
 * 手机格式验证器
 * @author feizns
 * @since 2023/2/5
 */
@RequiredArgsConstructor
public class AccountConstraintValidator extends AbstractConstraintValidator<Account, String> {

    /**
     * 账号正则
     */
    private Pattern pattern;

    @Override
    public void initialize(Account constraintAnnotation) {
        super.initialize(constraintAnnotation);
        pattern = Pattern.compile(annotation.regexp());
    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        return Strings.hasText(value)
                && value.length() >= annotation.min()
                && value.length() <= annotation.max()
                && pattern.matcher(value).matches();
    }

}
